<template>
  <div>
    <el-form
      :model="list"
      label-width="auto"
      :rules="rules"
      ref="formRef"
      style="max-width: 600px;margin-top: 10px">
      <el-form-item
        label="姓名"
        prop="realName"
      >
        <el-input
          v-model="this.cons.realName"
          disabled="disabled"
          type="text"
          autocomplete="off"
        />
      </el-form-item>

      <el-form-item
        label="申请时间"
        prop="applicationDate"
      >
        <el-date-picker
          v-model="list.applicationDate"
          type="date"
        />
      </el-form-item>


      <el-form-item
        label="部门"
        prop="deptName"
      >
        <el-input
          v-model="list.deptName"
          disabled="disabled"
          type="text"
        />
      </el-form-item>


      <el-form-item
        label="用章类型"
        prop="sealName"
      >
        <el-select v-model="list.sealName" placeholder="请选择">
          <el-option label="公司公章" value="公司公章"/>
          <el-option label="财务章" value="财务章"/>
          <el-option label="人事章" value="人事章"/>
          <el-option label="合同专用章" value="合同专用章"/>
        </el-select>
      </el-form-item>

      <el-form-item
        label="用章原因"
        prop="reason"
      >
        <el-input
          v-model="list.reason"
          type="textarea"
          autocomplete="off"
        />
      </el-form-item>

      <el-form-item
        label="下一步审批人"
        prop="manager"
      >
        <el-input
          v-model="list.manager"
          disabled="disabled"
          type="text"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="qx()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import con from "js-cookie";
import {queryDepartment, submitForm} from "@/api/sealManagement/table";

export default {

  data() {
    return {
      list: [],
      cons: JSON.parse(con.get("SET_USERS")),
      rules: {
        roomName: [
          {required: true, message: '姓名为必填项', trigger: 'blur'},
          {len: true, message: "长度2-10", trigger: "blur"}
        ],
        applicationDate: [
          {required: true, message: '申请时间为必填项', trigger: 'blur'}
        ],
        deptName: [
          {required: true, message: '部门为必填项', trigger: 'blur'}
        ],
        sealName: [
          {required: true, message: '用章类型为必填项', trigger: 'blur'}
        ],
        reason: [
          {required: true, message: '用章原因为必填项', trigger: 'blur'}
        ],
        manager: [
          {required: true, message: '必填', trigger: 'blur'},
        ],
      },
    }
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      console.log(this.cons)
      queryDepartment(this.cons.realName).then(response => {
        console.log(response)
        this.list = response.data[0]
        console.log(this.list)
      })
    },
    submitForm() {
      this.$refs.formRef.validate((valid) => {
        console.log(valid)
        if (valid) {
          console.log(this.list)
          var date = new Date(this.list.applicationDate);
          this.list.applicationDate = date.getFullYear() + "-"
            + (date.getMonth() + 1) + "-" + date.getDate() + " "
            + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()
          this.list.userID = this.cons.userID
          submitForm(this.list).then(data => {
            if (data.data.result) {
              alert("新增成功")
            } else {
              alert("新增失败")
            }
            window.location.reload();
          })
        } else {
          alert('新增失败');
          return false;
        }
      });

    }
  }
}
</script>

